<template>
	<view class="assetMain">
		<view class="assetList">
			<view class="assetListItem" v-for="(item,index) in assetsList" :key="index">
				<view class="itemContent">
					<view class="conCenter">
						<view style="font-weight: bold;">资产名称：{{item.assetName}}
							<view class="itemHeadState">
								{{item.statusCh?item.statusCh:'--'}}
							</view>
						</view>
						<view>申请编号：{{item.assetApplyNo}}</view>
						<view>规格型号：{{item.specification}}</view>
						<view>资产类型：{{item.assetTypeName?item.assetTypeName:'--'}}</view>
						<view>资产编号：{{item.assetNo}}</view>
						<view>实施类型：{{item.execTypeCh}}</view>
						<view>实施人：{{item.executorName?item.executorName:'--'}}
							<view v-if="item.statusCh=='待确认'"
								:class="{'itemHand':true,'permission-none':isPermission('ASS_IMP_CFM')}">
								<view class="color-g-bg" @click="toSign(item.assetExecDtlId,item.version)">
									<uni-icons type="compose" size="16" color="#fff"></uni-icons>&nbsp;&nbsp;确认签收
								</view>
							</view>
						</view>
					</view>
				</view>

			</view>
			<!-- 加载结束 -->
			<view class="uni-loadmore" v-if="showLoadMore">{{loadMoreText}}</view>
		</view>
		<MessageTip ref="messageTip" :tipContent="tipContent" @clickType="handleShowOrHideTip" />

	</view>
</template>

<script lang="ts">
	import {
		onPullDownRefresh,
		onReachBottom,
		onLoad
	} from "@dcloudio/uni-app";
	import {
		defineComponent,
		ref,
		reactive
	} from 'vue'
	import {
		cfrList,
		assetSign
	} from '@/common/api/assetAPI.ts'
	import {
		CONSTANT
	} from '@/common/config/config.ts'
	import {
		fromSearch
	} from '@/common/util/tabSearchUse.ts'
	import {
		isPermission
	} from '@/common/util/common.ts'

	export default defineComponent({

		setup(props, {
			emit
		}) {
			const tipContent = ref('') //提示信息
			const messageTip = ref()
			const showLoadMore = ref(false);
			const loadMoreText = ref('加载中……'); //加载更多提示
			const assetsList = ref([]); //资产列表数据

			const page = ref(CONSTANT.initPage); //初始加载页码
			const allPages = ref(0); //总页码数
			onPullDownRefresh(() => {
				console.log("触发下拉");
				// uni.startPullDownRefresh(); //下拉开启
				page.value = CONSTANT.initPage; //重置页码
				assetsList.value = []; //重置列表
				getList();
			});
			onReachBottom(() => {
				//触发底部
				if (page.value > allPages.value || page.value == allPages.value) {
					loadMoreText.value = '没有更多的数据了'
					showLoadMore.value = true;
				} else {
					page.value++;
					loadMoreText.value = '加载中……';
					showLoadMore.value = true;
					getList();
				}

			})
			//获取列表数据
			const searchParams = ref({
				status: 7,
				assetApplyType: 1,
				mainApplyId: null
			}); //查询条件
			const matchRule = {
				status: 'eq',
				assetApplyType: 'eq',
				mainApplyId: 'eq'
			}; //匹配条件

			onLoad((par) => {
				if (par.assetApplyId) {
					searchParams.value.mainApplyId = par.assetApplyId
				}
				getList();
			})

			function getList() {
				let config = {
					page: page.value,
					rows: CONSTANT.pageSize
				}
				const params = Object.assign(fromSearch(searchParams.value, matchRule), config)
				if (!isPermission('ASS_IMP_LIST_QUERY')) {
					cfrList(params).then((res: any) => {
						uni.stopPullDownRefresh(); //关闭下拉
						if (res.code === 0) {
							allPages.value = res.data.pages;
							page.value = res.data.current;
							let list: any[] = res.data.records;
							assetsList.value = assetsList.value.concat(list);
							if (assetsList.value.length === 0) {
								showLoadMore.value = true;
								loadMoreText.value = '暂无数据'
							} else if (assetsList.value.length < CONSTANT.pageSize) {
								showLoadMore.value = true;
								loadMoreText.value = '没有更多的数据了'
							} else {
								showLoadMore.value = false;
							}
						}

					})
				} else {
					showLoadMore.value = true;
					loadMoreText.value = '暂无数据'
				}

			}

			// 确认收到
			const signParams = ref({})
			const toSign = (assetExecDtlId, version) => {
				tipContent.value = '确认已到货？'
				signParams.value = {assetExecDtlId,version};
				
				messageTip.value.openTip()

			}
			
			const handleShowOrHideTip = (type: string) => {
				if (type == 'yes') {
					assetSign([{
						assetExecDtlId: signParams.value.assetExecDtlId,
						version: signParams.value.version,
						status: 8, //已确认状态
						confirmResult: '',
					}]).then(value => {
						if (value.code == 0) {
							uni.showToast({
								icon: 'none',
								title: '签收成功',
								duration:500
							})
							setTimeout(() => {
								assetsList.value = []
								getList();
							}, 500)

						} else {
							uni.showToast({
								icon: 'none',
								title: '签收失败！'
							})
						}
					})
				}
			}
			return {
				showLoadMore,
				loadMoreText,
				assetsList,
				searchParams,
				toSign,
				isPermission,
				tipContent,
				handleShowOrHideTip,
				messageTip
			}
		},
	})
</script>

<style scoped lang="scss">
	.assetMain {
		background-color: #eee;
		font-size: 14px;
	}

	.assetNav {
		display: flex;
		flex-wrap: row nowrap;
		justify-content: space-around;
		padding: 15px;
		background-color: #fff;
	}

	.assetList {
		padding: 10px;
	}

	.itemHead {
		position: relative;
	}

	.assetListItem {
		margin-top: 15px;
		background-color: #fff;
		border-radius: 5px;
	}

	.itemHeadState {
		float: right;
		display: inline-block;
		border: 1px solid #f6a031;
		font-size: 11px;
		color: #f6a031;
		padding: 3px 12px;
		border-radius: 15px;
		font-weight: normal;
	}

	.itemContent {
		font-size: 15px;
		box-sizing: border-box;
		padding: 15px 10px;
		border-bottom: 1px solid #eee;
		flex-wrap: row nowrap;
	}

	.itemContent>view:nth-child(1) {
		padding-left: 15px;
		color: #333;
	}

	.itemContent>view:nth-child(1)>view {
		padding: 2px 0;
		text-overflow: ellipsis;
		white-space: nowrap;
		overflow: hidden;
		cursor: pointer;
	}

	.itemHand {
		display: inline-block;
		float: right;
	}

	.itemHand>view {
		padding: 4px 25px;
		font-size: 12px;
		border-radius: 15px;
	}

	.buttons {
		padding: 10px;
		display: flex;
		flex-flow: row nowrap;
		justify-content: space-around;
		font-size: 15px;
	}

	// .buttons * {
	// 	font-size: 15px;
	// }
</style>
